package com.ruoyi.vip.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.vip.domain.TVip;
import com.ruoyi.vip.service.ITVipService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会员管理Controller
 * 
 * @author ruoyi
 * @date 2024-11-09
 */
@RestController
@RequestMapping("/vip/vip")
public class TVipController extends BaseController
{
    @Autowired
    private ITVipService tVipService;

    /**
     * 查询会员管理列表
     */
    @PreAuthorize("@ss.hasPermi('vip:vip:list')")
    @GetMapping("/list")
    public TableDataInfo list(TVip tVip)
    {
        startPage();
        List<TVip> list = tVipService.selectTVipList(tVip);
        return getDataTable(list);
    }

    /**
     * 导出会员管理列表
     */
    @PreAuthorize("@ss.hasPermi('vip:vip:export')")
    @Log(title = "会员管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TVip tVip)
    {
        List<TVip> list = tVipService.selectTVipList(tVip);
        ExcelUtil<TVip> util = new ExcelUtil<TVip>(TVip.class);
        util.exportExcel(response, list, "会员管理数据");
    }

    /**
     * 获取会员管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('vip:vip:query')")
    @GetMapping(value = "/{vipId}")
    public AjaxResult getInfo(@PathVariable("vipId") Long vipId)
    {
        return success(tVipService.selectTVipByVipId(vipId));
    }

    /**
     * 新增会员管理
     */
    @PreAuthorize("@ss.hasPermi('vip:vip:add')")
    @Log(title = "会员管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TVip tVip)
    {
        int rows = tVipService.insertTVip(tVip);
        if (rows == 1) {
            return success();
        }else if(rows == 2){
            return error("该用户未注册");
        }else {
            return error("该用户已成为VIP");
        }
    }

    /**
     * 修改会员管理
     */
    @PreAuthorize("@ss.hasPermi('vip:vip:edit')")
    @Log(title = "会员管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TVip tVip)
    {
        int rows = tVipService.updateTVip(tVip);
        if (rows == 1) {
            return success();
        }else if(rows == 2){
            return error("该用户未注册");
        }else {
            return error("该用户已成为VIP");
        }
    }

    /**
     * 删除会员管理
     */
    @PreAuthorize("@ss.hasPermi('vip:vip:remove')")
    @Log(title = "会员管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{vipIds}")
    public AjaxResult remove(@PathVariable Long[] vipIds)
    {
        return toAjax(tVipService.deleteTVipByVipIds(vipIds));
    }
}
